package com.zzyl.nursing.controller;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.nursing.dto.CancelDto;
import com.zzyl.nursing.dto.ExecuteTaskDTO;
import com.zzyl.nursing.dto.UpdateTaskTimeDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;


/**
 * 护理任务Controller
 *
 * @author ruoyi
 * @date 2024-09-27
 */
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务的接口")
public class NursingTaskController extends BaseController {
    @Autowired
    private INursingTaskService nursingTaskService;

    /**
     * 查询护理任务列表
     */
    @ApiOperation("查询护理任务列表")
    @GetMapping("/list")
    public TableDataInfo list(NursingTask nursingTask) {
        startPage();
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
        return getDataTable(list);
    }

    /**
     * 导出护理任务列表
     */
    @ApiOperation("导出护理任务列表")
    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, NursingTask nursingTask) {
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
        util.exportExcel(response, list, "护理任务数据");
    }

    /**
     * 获取护理任务详细信息
     */
    @ApiOperation("获取护理任务详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "护理任务ID", required = true)
                              @PathVariable("id") Long id) {
        return success(nursingTaskService.selectNursingTaskById(id));
    }

    /**
     * 新增护理任务
     */
    @ApiOperation("新增护理任务")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "护理任务实体")
                          @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 修改护理任务
     */
    @ApiOperation("修改护理任务")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "护理任务实体")
                           @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @ApiOperation("删除护理任务")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }


    @ApiOperation("取消任务")
    @PutMapping("/cancel")
    public AjaxResult cancel(@RequestBody CancelDto cd) {
        nursingTaskService.cancel(cd);
        //return toAjax(nursingTaskService.updateNursingTask(nursingTask));
        return success();
    }







    /**
     * 执行护理任务
     */
    @ApiOperation("执行护理任务")
    @PutMapping("/do")
    public AjaxResult doTask(@RequestBody ExecuteTaskDTO executeTaskDTO) {
        // 调用服务层方法执行任务
        nursingTaskService.doTask(executeTaskDTO);

        return success();
    }

    @ApiOperation("上传任务图片")
    @PostMapping("/uploadImage/{taskId}")
    public AjaxResult uploadImage(@PathVariable Long taskId, @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("文件为空");
        }

        try {
            // 保存文件到指定路径
            Path path = Paths.get("uploads", file.getOriginalFilename());
            Files.createDirectories(path.getParent());
            Files.copy(file.getInputStream(), path);

            // 更新任务图片路径
            String imagePath = path.toAbsolutePath().toString();
            int result = nursingTaskService.updateTaskImage(taskId, imagePath);

            return result > 0 ? AjaxResult.success("图片上传成功") : AjaxResult.error("图片上传失败");
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("图片上传失败: " + e.getMessage());
        }
    }

    @ApiOperation("更新任务时间")
    @PutMapping("/updateTime")
    public AjaxResult updateTime(@RequestBody UpdateTaskTimeDTO updateTaskTimeDTO) {
        // 检查预计服务时间是否为 null
        if (updateTaskTimeDTO.getEstimatedServerTime() == null) {
            return AjaxResult.error("预计服务时间不能为空");
        }

        // 调用服务层方法更新任务时间
        int result = nursingTaskService.updateTime(updateTaskTimeDTO);

        return result > 0 ? AjaxResult.success("更新成功") : AjaxResult.error("更新失败");
    }

}

